package org.jeecg.modules.recruit.appuser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.api.dto.req.UserPageReq;
import org.jeecg.api.dto.resp.position.MMUserWorkCollectionResultDto;
import org.jeecg.modules.recruit.appuser.entity.MmUserPositionCollection;
import org.jeecg.modules.recruit.appuser.mapper.MmUserPositionCollectionMapper;
import org.jeecg.modules.recruit.appuser.service.IMmUserPositionCollectionService;
import org.jeecg.modules.recruit.company.entity.MmCompanyMain;
import org.jeecg.modules.recruit.company.service.IMmCompanyMainService;
import org.jeecg.modules.recruit.position.entity.MmCompanyPosition;
import org.jeecg.modules.recruit.position.service.IMmCompanyPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 职位收藏记录
 * @Author: jeecg-boot
 * @Date: 2020-08-28
 * @Version: V1.0
 */
@Service
public class MmUserPositionCollectionServiceImpl extends ServiceImpl<MmUserPositionCollectionMapper, MmUserPositionCollection> implements IMmUserPositionCollectionService {

    @Autowired
    private IMmCompanyPositionService mmCompanyPositionService;

    @Autowired
    private IMmCompanyMainService mmCompanyMainService;

    @Override
    public Page<MMUserWorkCollectionResultDto> getUserCollectionList(UserPageReq req) {
        IPage<MmUserPositionCollection> page = this.lambdaQuery()
                .eq(MmUserPositionCollection::getUserId, req.getUserId())
                .page(new Page<>(req.getPage(), req.getPageSize()));
        List<MMUserWorkCollectionResultDto> collect = page.getRecords().stream().map(sign -> {
            MMUserWorkCollectionResultDto resultDto = new MMUserWorkCollectionResultDto();
            resultDto.setId(sign.getId());
            resultDto.setCreateTime(sign.getUpdateTime());
            resultDto.setPositionId(sign.getPositionId());
            // 获取职位信息
            MmCompanyPosition position = mmCompanyPositionService.getById(sign.getPositionId());
            if (position != null) {
                resultDto.setSalaryMin(position.getSalaryMin());
                resultDto.setSalaryMax(position.getSalaryMax());
                resultDto.setPsName(position.getPsName());
                //获取公司
                MmCompanyMain companyMain = mmCompanyMainService.getById(position.getCompanyId());
                if (companyMain != null) {
                    resultDto.setCompanyName(companyMain.getCpName());
                    resultDto.setCpPhone(companyMain.getCpPhone());
                    resultDto.setCpLocation(companyMain.getCpLocation());
                }
            }

            return resultDto;
        }).collect(Collectors.toList());
        Page<MMUserWorkCollectionResultDto> resultDtoPage = new Page<>(page.getCurrent(), req.getPageSize(), page.getTotal());
        resultDtoPage.setRecords(collect);
        return resultDtoPage;

    }
}
